package com.yue.yadmin.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.yue.yadmin.utils.SpringContextUtils;
import com.yue.yadmin.utils.VelocityUtils;

//$dataTables列表结构{
// 	id:dataTables表格的id，自动生成(datatables_时间戳)
// 	listUrl:请求列表数据的地址，非必填 默认${request.contextPath}$!{module}/pageList
// 	isDelete:是否有记录删除功能，非必填 true|false ，默认false
// 	deleteUrl:记录删除请求地址，非必填 默认${request.contextPath}$!{module}/delete
// 	addUrl:添加记录请求地址，非必填 默认${request.contextPath}$!{module}/add
// 	updateUrl:修改记录请求地址，非必填 默认${request.contextPath}$!{module}/pageList
// 	fields 字段集合[
// 		字段{
//			data:字段key，必填
//			title:字段显示名称，必填
//			isShow:列表是否展示字段，非必填，true|false，默认false
//			isAdd:该字段是否用于添加记录使用，非必填，true|false，默认false		
//			isUpdate:该字段是否用于修改记录使用，非必填，true|false，默认false		
//			isSearch:该字段是否用于查询记录使用，非必填，true|false，默认false		
//			isNeed:该字段是否增改必要参数，非必填，true|false，默认false
//			dataType:字段类型，主要用于特殊处理字段，非必填(时间类型用dataType=date)
//			defaultValue:默认值，非必填 (如dataType="date",defaultValue="2018-07-10 13:42:30")
//			dataSources:数据源，非必填，如果存在则只能从数据源选择，页面中元素为select下拉选
//			render:列表数据渲染，非必填，(JavaScript脚本 ,如"render"="function(data,type,row,meta) {return '<i class=\''+row['icon']+'\'>'+row['icon']+'</i>';}")
// 		}
// 	]
// }
public class DataTablesModel implements Serializable{
	private static final long serialVersionUID = 1L;
	private String module;
	private String id;
	private String listUrl;
	private boolean isDelete;
	private String deleteUrl;
	private String addUrl;
	private String updateUrl;
	private List<Field> fields;
	
	public DataTablesModel(String module, boolean isDelete, List<Field> fields) {
		super();
		this.module = StringUtils.isBlank(module)?"":module;
		this.id = VelocityUtils.getDataTablesId();
		String prefixUrl = SpringContextUtils.getRequest().getContextPath()+this.module;
		this.listUrl = prefixUrl+"/pageList";
		this.isDelete = isDelete;
		this.deleteUrl = prefixUrl+"/delete";
		this.addUrl = prefixUrl+"/add";
		this.updateUrl = prefixUrl+"/update";
		this.fields = fields;
	}
	
	public DataTablesModel(String module, String id, String listUrl, boolean isDelete, String deleteUrl, String addUrl,
			String updateUrl, List<Field> fields) {
		super();
		this.module = module;
		this.id = id;
		this.listUrl = listUrl;
		this.isDelete = isDelete;
		this.deleteUrl = deleteUrl;
		this.addUrl = addUrl;
		this.updateUrl = updateUrl;
		this.fields = fields;
	}

	public String getModule() {
		return module;
	}

	public void setModule(String module) {
		this.module = module;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getListUrl() {
		return listUrl;
	}

	public void setListUrl(String listUrl) {
		this.listUrl = listUrl;
	}

	public boolean getIsDelete() {
		return isDelete;
	}

	public void setIsDelete(boolean isDelete) {
		this.isDelete = isDelete;
	}

	public String getDeleteUrl() {
		return deleteUrl;
	}

	public void setDeleteUrl(String deleteUrl) {
		this.deleteUrl = deleteUrl;
	}

	public String getAddUrl() {
		return addUrl;
	}

	public void setAddUrl(String addUrl) {
		this.addUrl = addUrl;
	}

	public String getUpdateUrl() {
		return updateUrl;
	}

	public void setUpdateUrl(String updateUrl) {
		this.updateUrl = updateUrl;
	}

	public List<Field> getFields() {
		return fields;
	}

	public void setFields(List<Field> fields) {
		this.fields = fields;
	}
	
	public DataTablesModel addField(Field field){
		if(this.fields==null)this.fields = new ArrayList<Field>();
		this.fields.add(field);
		return this;
	}

	public static class Field implements Serializable{
		private static final long serialVersionUID = 1L;
		private String data;
		private String title;
		private boolean isShow;
		private boolean isAdd;
		private boolean isUpdate;
		private boolean isSearch;
		private boolean isNeed;
		private String dataType;
		private Object defaultValue;
		private List<Map<String,Object>> dataSources;
		private String render;
		
		public Field(String data, String title, boolean isShow, boolean isAdd, boolean isUpdate, boolean isSearch,
				boolean isNeed, String dataType) {
			super();
			this.data = data;
			this.title = title;
			this.isShow = isShow;
			this.isAdd = isAdd;
			this.isUpdate = isUpdate;
			this.isSearch = isSearch;
			this.isNeed = isNeed;
			this.dataType = dataType;
		}
		
		public Field(String data, String title, boolean isShow, boolean isAdd, boolean isUpdate, boolean isSearch,
				boolean isNeed, String dataType, Object defaultValue, List<Map<String, Object>> dataSources,
				String render) {
			super();
			this.data = data;
			this.title = title;
			this.isShow = isShow;
			this.isAdd = isAdd;
			this.isUpdate = isUpdate;
			this.isSearch = isSearch;
			this.isNeed = isNeed;
			this.dataType = dataType;
			this.defaultValue = defaultValue;
			this.dataSources = dataSources;
			this.render = render;
		}
		public String getData() {
			return data;
		}
		public void setData(String data) {
			this.data = data;
		}
		public String getTitle() {
			return title;
		}
		public void setTitle(String title) {
			this.title = title;
		}
		public boolean getIsShow() {
			return isShow;
		}
		public void setIsShow(boolean isShow) {
			this.isShow = isShow;
		}
		public boolean getIsAdd() {
			return isAdd;
		}
		public void setIsAdd(boolean isAdd) {
			this.isAdd = isAdd;
		}
		public boolean getIsUpdate() {
			return isUpdate;
		}
		public void setIsUpdate(boolean isUpdate) {
			this.isUpdate = isUpdate;
		}
		public boolean getIsSearch() {
			return isSearch;
		}
		public void setIsSearch(boolean isSearch) {
			this.isSearch = isSearch;
		}
		public boolean getIsNeed() {
			return isNeed;
		}
		public void setIsNeed(boolean isNeed) {
			this.isNeed = isNeed;
		}
		public String getDataType() {
			return dataType;
		}
		public void setDataType(String dataType) {
			this.dataType = dataType;
		}
		public Object getDefaultValue() {
			return defaultValue;
		}
		public void setDefaultValue(Object defaultValue) {
			this.defaultValue = defaultValue;
		}
		public List<Map<String, Object>> getDataSources() {
			return dataSources;
		}
		public void setDataSources(List<Map<String, Object>> dataSources) {
			this.dataSources = dataSources;
		}
		public String getRender() {
			return render;
		}
		public void setRender(String render) {
			this.render = render;
		}
		@Override
		public String toString() {
			return "Field [data=" + data + ", title=" + title + ", isShow=" + isShow + ", isAdd=" + isAdd
					+ ", isUpdate=" + isUpdate + ", isSearch=" + isSearch + ", isNeed=" + isNeed + ", dataType="
					+ dataType + ", defaultValue=" + defaultValue + ", dataSources=" + dataSources + ", render="
					+ render + "]";
		}
		
	}

	@Override
	public String toString() {
		return "DataTablesModel [module=" + module + ", id=" + id + ", listUrl=" + listUrl + ", isDelete=" + isDelete
				+ ", deleteUrl=" + deleteUrl + ", addUrl=" + addUrl + ", updateUrl=" + updateUrl + ", fields=" + fields
				+ "]";
	}
	
}
